package com.hejia.alauda.entity;

import java.util.Date;

import javax.persistence.Column;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;

import com.hejia.alauda.utils.Encryption;

/**
 * 用户类
 * Created by Johnny(johnny@hejiajinrong.com) on 14-8-28.
 */
public class User extends BaseEntity{

    /**
	 * 
	 */
	private static final long serialVersionUID = -8395548795651373416L;
	
	public interface TradeRecordView {};

	@Column
    private String realName; //实名

    @Column
    private String mobile; //手机号码
    
    @JsonIgnore
    @Column
    private String idCard; //身份证
    
    @JsonIgnore
    @Column
    private String password; //密码
    
    @Column
    private String userKey; //用户KEY，主要用于移动端
    
    @Column
    private String referee; //推荐人或用户来源
    
    @Column
    private String subReferee; //下一级用户来源

	@Column
    private String openId; //微信的openId
    
    @Column
    private String external; //用户外部使用ID
    
    @Column
    private String ip; //注册ip
    
    @Column
    private Date refreshDate;
    
    @Column
    private UserRank rank;
    
//    @Column
//    private String credit51_id;
    
//    private Balance balance;
//    @JsonIgnore
//    @Column
//    private String payPassword; //支付密码
    
//    @Column
//    private BigDecimal income; //用户的总收益
    
    
    
    public enum UserRank{
    	VIP5, VIP4, VIP3, VIP2, VIP1
    }

    public String getRealName() {
    	if (realName != null) {
    		return realName.trim();
    	}
        return realName;
    }
    
    

    public UserRank getRank() {
		return rank;
	}



	public void setRank(UserRank rank) {
		this.rank = rank;
	}



	public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserKey() {
		return userKey;
	}

	public void setUserKey(String userKey) {
		this.userKey = userKey;
	}

	public String getReferee() {
		return referee;
	}

	public void setReferee(String referee) {
		this.referee = referee;
	}
	
	public String getSubReferee() {
		return subReferee;
	}

	public void setSubReferee(String subReferee) {
		this.subReferee = subReferee;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getExternal() {
		return external;
	}

	public void setExternal(String external) {
		this.external = external;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
	
	
	public String getProtectedMobile(){
		if(StringUtils.isBlank(this.mobile)){
			return "";
		}
		return Encryption.obfuscatedMobile(mobile);
	}
	
	/**
	 * 获取保护的实名
	 * @return	保护的实名
	 */
	public String getProtectedRealName(){
		
		if(StringUtils.isBlank(this.realName)){
			return "";
		}
		
//		String name = "";
//		int length = this.realName.length();
//		if(length == 1){
//			name = this.realName;
//		}else if(length == 2){
//			name = Character.toString(this.realName.charAt(0)) + "*";
//		}else if(length == 3){
//			name = Character.toString(this.realName.charAt(0)) + "*" + Character.toString(this.realName.charAt(2));
//		}else if(length >= 4){
//			name = this.realName.substring(0, 2);
//			for(int i = 2; i < length - 1; i++){
//				name += "*";
//			}
//			name += this.realName.substring(length -1 , length);
//		}
		return Encryption.obfuscatedName(realName);
	}
	
	/**
	 * 获取保护的公民身份号码
	 * @return	保护的公民身份号码
	 */
	public String getProtectedIdCard(){
		
		if(StringUtils.isBlank(this.idCard)){
			return "";
		}
		
		int length = this.idCard.length();
		if(length <= 8){
			return this.idCard;
		}
		
		String protectedIdCard = this.idCard.substring(0, 4);
		protectedIdCard = StringUtils.rightPad(protectedIdCard, length - 4, '*');
		protectedIdCard += this.idCard.substring(length - 4, length);
		
		return protectedIdCard;
	}

	public Date getRefreshDate() {
		return refreshDate;
	}

	public void setRefreshDate(Date refreshDate) {
		this.refreshDate = refreshDate;
	}


	public boolean isVip(){
		return  UserRank.VIP1.equals(this.getRank()) ||
				UserRank.VIP2.equals(this.getRank()) ||
				UserRank.VIP3.equals(this.getRank()) ||
				UserRank.VIP4.equals(this.getRank()) ||
				UserRank.VIP5.equals(this.getRank());
	}

	@Override
	public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this){
            return true;
        }
        if (getClass() != obj.getClass()){
            return false;
        }
        User user = (User)obj;
        return (this.getId() == user.getId());
    }
	
	
}
